<template>
  <nav class="site-navbar" :class="'site-navbar--' + navbarLayoutType">
    <div class="site-navbar__header">
      <h1 class="site-navbar__brand" @click="$router.push({ name: 'home' })">
        <a class="site-navbar__brand-lg" href="javascript:;">同城外卖3.0系统</a>
        <a class="site-navbar__brand-mini" href="javascript:;">
          <img src="~@/assets/img/avatar.png" style="width: 25px;border-radius: 50%;">
        </a>
      </h1>
    </div>
    <div class="site-navbar__body clearfix">
      <el-menu class="site-navbar__menu" mode="horizontal">
        <el-menu-item class="site-navbar__switch" index="0" @click="sidebarFold = !sidebarFold">
          <icon-svg name="zhedie"></icon-svg>
        </el-menu-item>
      </el-menu>
      <el-menu class="site-navbar__menu site-navbar__menu--right" mode="horizontal">
        <!-- <el-menu-item index="1" style="position: relative;" @click="$router.push({ name: 'missionSm' })" v-if="show">
          <template slot="title">
            <span class="left-number" v-if="chatNum">{{chatNum}}</span>
            <icon-svg name="xiaoxi" class="el-icon-setting" style="font-size: 28px;"></icon-svg>
            <audio id="audio1" autoplay="autoplay" v-if="chatNum>0"
              src="https://pw.xianmxkj.com/file/uploadPath/2022/01/12/01beb0bc15549b9aa4d18027a7751bae.mp3" ref='audio'
              controls hidden></audio>
          </template>
        </el-menu-item> -->
        <el-menu-item index="1" style="position: relative;" @click="$router.push({ name: 'vueMchat' })">
          <template slot="title">
            <span class="left-number" v-if="chatNum2">{{chatNum2}}</span>
            <icon-svg name="xiaoxi" class="el-icon-setting" style="font-size: 28px;"></icon-svg>
            <audio id="audio" autoplay="autoplay" v-if="chatNum2==1"
              src="https://pw.xianmxkj.com/file/uploadPath/2022/01/19/0753211f78d718d44ee6372e33eae9ee.mp3" ref='audio'
              controls hidden></audio>
          </template>
        </el-menu-item>
        <!-- <el-menu-item index="1" @click="$router.push({ name: 'theme' })">
          <template slot="title">
            <el-badge value="new">
              <icon-svg name="shezhi" class="el-icon-setting"></icon-svg>
            </el-badge>
          </template>
        </el-menu-item> -->
        <el-menu-item class="site-navbar__avatar" index="3">
          <el-dropdown :show-timeout="0" placement="bottom">
            <span class="el-dropdown-link">
              <img src="~@/assets/img/avatar.png" :alt="userName">{{ userName }}
            </span>
            <el-dropdown-menu slot="dropdown">
              <!-- <el-dropdown-item @click.native="updatePasswordHandle()">修改密码</el-dropdown-item> -->
              <el-dropdown-item @click.native="logoutHandle()">退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password v-if="updatePassowrdVisible" ref="updatePassowrd"></update-password>
  </nav>
</template>

<script>
  import UpdatePassword from './main-navbar-update-password'
  import {
    clearLoginInfo
  } from '@/utils'
  export default {
    data() {
      return {
        updatePassowrdVisible: false,
        chatNum: 0,
        chatNum2: 0,
        intervalId: 0,
        errCount: 0,
        shopIds: [],
        show: false,
        storeId: 0,
      }
    },
    components: {
      UpdatePassword
    },
    computed: {
      navbarLayoutType: {
        get() {
          return this.$store.state.common.navbarLayoutType
        }
      },
      sidebarFold: {
        get() {
          return this.$store.state.common.sidebarFold
        },
        set(val) {
          this.$store.commit('common/updateSidebarFold', val)
        }
      },
      mainTabs: {
        get() {
          return this.$store.state.common.mainTabs
        },
        set(val) {
          this.$store.commit('common/updateMainTabs', val)
        }
      },
      userName: {
        get() {
          return this.$store.state.user.name
        }
      }
    },
    mounted() {
      this.chatSelect()
      var userId = this.$cookie.get('userId')
      this.$http({
        url: this.$http.adornUrl(`sys/user/info`),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({
        data
      }) => {
        if (data && data.code === 0) {
          var shows = 1
          for (var i in data.user.roleIdList) {
            if (data.user.roleIdList[i] == 12) {
              shows = 2
            } else {
              shows = 1
            }
          }
          console.log('shows', shows)
          if (shows == 2) {
            this.show = true
          }
          if (shows == 1) {
            this.show = false
          }

          if (this.show == true) {
            this.dataSelect()
          }
        }
      })


    },
    methods: {
      // 修改密码
      updatePasswordHandle() {
        this.updatePassowrdVisible = true
        this.$nextTick(() => {
          this.$refs.updatePassowrd.init()
        })
      },
      // 退出
      logoutHandle() {
        this.$confirm(`确定进行[退出]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('sys/logout'),
            method: 'post',
            data: this.$http.adornData()
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              clearLoginInfo()
              this.$cookie.set('token', '')
              sessionStorage.clear()
              clearInterval(this.chatNum)
              clearInterval(this.chatNum2)
              this.$router.push({
                name: 'login'
              })
            }
          })
        }).catch(() => {})
      },
      // 消息提醒
      newsSelect() {
        var _this = this
        this.$http({
          url: this.$http.adornUrl('admin/ordermessage/selectReadFlagCount'),
          method: 'post',
          params: this.$http.adornParams({
            'shopIds': this.shopIds.toString()
          })
        }).then(({
          data
        }) => {
          if (data.code == 0) {
            if (data.data > 0) {
              if (this.errCount != data.data) {
                this.$notify.info({
                  title: '消息',
                  dangerouslyUseHTMLString: true,
                  message: '有' + data.data + '条订单，请前往订单中心处理。',
                  offset: 80,
                  duration: 0,
                  type: "warning",
                  onClose() {
                    _this.defineCallBack();
                  }
                });
                this.errCount = data.data
                this.chatNum = data.data
              }
            } else {
              this.errCount = 0
              this.chatNum = 0
            }
          }

        })
      },
      defineCallBack() {
        this.$http({
          url: this.$http.adornUrl('admin/ordermessage/allcheckOrderMessage'),
          method: 'post',
          params: this.$http.adornParams({
            'shopIds': this.shopIds.toString()
          })
        }).then(({
          data
        }) => {
          if (data.code === 0) {
            this.errCount = 0
            this.chatNum = 0
            this.$router.replace({
              name: 'missionSm'
            })
          }
          console.log('清空请求', data.data)
        })


      },
      aplayAudio() {
        const audio = document.getElementById('audio')
        audio.play()
        console.log('语音提示')
      },
      // 获取店铺数据
      dataSelect() {
        this.tableDataLoading = true
        this.$http({
          url: this.$http.adornUrl('admin/goods/selectAllShop'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 10,
            'shopName': ''
          })
        }).then(({
          data
        }) => {
          this.tableDataLoading = false
          var arr = []
          for (var i in data.data.list) {
            if (data.data.list[i].shopId) {
              arr.push(data.data.list[i].shopId)
            }
          }
          let returnData = data.data
          this.shopIds = arr
          this.newsSelect()
        })
      },
      // 获取聊天消息
      chatSelect() {
        this.$http({
          url: this.$http.adornUrl('chats/count'),
          method: 'get',
          params: this.$http.adornParams({
            'storeId': this.storeId
          })
        }).then(({
          data
        }) => {
          this.code = data.code

          if (data.data > 0) {
            if (this.errCount != data.data) {
              this.$notify.info({
                title: '消息',
                dangerouslyUseHTMLString: true,
                message: '您有' + data.data + '条未读消息。',
                offset: 80,
                duration: 2500,
                type: "warning"
              });
              this.chatNum2 = data.data
              this.errCount = data.data
            }
          } else {
            this.chatNum2 = 0
            this.errCount = 0
          }
          if (this.chatNum == 0) {
            this.isshow = false
          } else {
            this.isshow = true
          }
          console.log('this.chatNum2', this.chatNum2)
        })
      },
    },
    created() {
      console.log('this.show', this.show)
      // if(this.show==true){
      // setInterval(() => {
      //   this.newsSelect()
      // }, 10000)
      // }
      // setInterval(() => {
      //   this.newsSelect()
      // }, 10000)
      this.chatNum2 = setInterval(() => {
        var token = this.$cookie.get('token')
        if (token !== '') {
          this.chatSelect()
        } else {
          clearInterval(this.chatNum2)
        }
      }, 6000)

    },
    beforeDestroy() {
      clearInterval(this.chatNum)
    },
    watch: {
      'chatNum': function() {
        this.aplayAudio()
      },
      'chatNum2': function() {
        this.aplayAudio()
      }
    }
  }
</script>
<style scoped="scoped">
  .site-navbar__avatar .el-dropdown-link>img {
    height: 36px;
  }

  .left-number {
    position: absolute;
    top: 0;
    right: 20px;
    width: 18px;
    height: 18px;
    line-height: 18px;
    border-radius: 50%;
    background: #ff4949;
    color: #ffffff;
    text-align: center;
    font-size: 10px;
    font-family: PingFang SC, PingFang SC-Regular;
    font-weight: 400;
    margin-top: 5px;
  }
</style>
